﻿Imports System.ServiceModel
Imports System.ServiceModel.Configuration
Imports System.Configuration
Imports System.Xml

<CallbackBehavior(ConcurrencyMode:=ConcurrencyMode.Multiple), ServiceBehavior(InstanceContextMode:=InstanceContextMode.Single, ConcurrencyMode:=ConcurrencyMode.Single)>
<GlobalAvalibleObjectImplementation(singleton:=True)>
Public Class NetClientNotificationReceiver
    Implements INetContentDispatcherConsumer

    Private pipeFactory_noti As DuplexChannelFactory(Of INetNotification)
    Private pipeProxy_noti As IClientChannel

    Private _server_noti As INetNotification

    Sub resetServer()
        Dim self_ref As EventHandler = AddressOf Me.resetServer

        If pipeProxy_noti IsNot Nothing Then
            RemoveHandler pipeProxy_noti.Faulted, self_ref
            RemoveHandler pipeProxy_noti.Closed, self_ref
        End If

        Dim group As ServiceModelSectionGroup = ServiceModelSectionGroup.GetSectionGroup(ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None))
        If group IsNot Nothing Then

            Try
                pipeFactory_noti = New DuplexChannelFactory(Of INetNotification)(Me, "NetNotificationEndpoint")
                'create a communication channel and register for its events
                pipeProxy_noti = pipeFactory_noti.CreateChannel()

                pipeProxy_noti.Open()

                AddHandler pipeProxy_noti.Faulted, self_ref
                AddHandler pipeProxy_noti.Closed, self_ref

                _server_noti = pipeProxy_noti

                _server_noti.registerConsumer()
            Catch ex As Exception
                MsgBox("sorry can't open " & GetType(INetNotification).Name)
            End Try
            
        End If
    End Sub

    Private _content_type_map As New Dictionary(Of String, IContentXml)

    Public Sub New()
        resetServer()

        For Each Type As Type In getMystery.AssemblyRegister.getTypesMarkedWith(Of ContentType)()
            Dim attr As ContentType = getMystery.AssemblyRegister.getMysteryClassAttribute(Of ContentType)(Type)
            _content_type_map(attr.name) = Type.getMysteryAttribute(Of ContentXmlAttr)().getXmler
        Next

    End Sub

    Public Event contentAdded(c_guid As Guid)

    Public Event contentChanged()

    Public Event contentRemoved(content As IContent)

    Public Event contentEdit(content As IContent)

    Public Sub contentAdded1(input As contentAddedInput) Implements INetContentDispatcherConsumer.contentAdded

        Dim c_guid As Guid = Guid.Parse(input.guid)
        RaiseEvent contentAdded(c_guid)
    End Sub

    Public Sub contentRemoved1(input As contentRemovedInput) Implements INetContentDispatcherConsumer.contentRemoved
        Dim doc As New XmlDocument
        doc.InnerXml = input.doc
        Dim xmlelement As XmlElement = doc.FirstChild
        Dim content_type_name As String = xmlelement.Name
        Dim result As IContent = _content_type_map(content_type_name).read(doc)
        RaiseEvent contentRemoved(result)
    End Sub

    Public Sub HelloWorld() Implements INetContentDispatcherConsumer.HelloWorld

    End Sub

    Public Sub contentEdit1(input As notificateEditInput) Implements INetContentDispatcherConsumer.contentEdit
        Dim doc As New XmlDocument
        doc.InnerXml = input.doc
        Dim xmlelement As XmlElement = doc.FirstChild
        Dim content_type_name As String = xmlelement.Name
        Dim result As IContent = _content_type_map(content_type_name).read(doc)
        RaiseEvent contentEdit(result)
    End Sub
End Class
